import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';

class AddAccountController extends GetxController {
  FocusNode codeFocus = FocusNode();
  FocusNode nameFocus = FocusNode();

  //公司id
  String superCompanyId = "";

  //账户id
  String accountId = "";

  //账户编号
  String accountCode = "";

  //账户编号
  String accountName = "";

  @override
  void onReady() {
    super.onReady();
    superCompanyId = Get.arguments ?? "";
  }

  void setAccountCode(String value) {
    accountCode = value;
    update();
  }

  void setAccountName(String value) {
    accountName = value;
    update();
  }

  void confirm() {
    if (accountCode.isEmpty || accountName.isEmpty) {
      return;
    }

    codeFocus.unfocus();
    nameFocus.unfocus();

    httpManager.post(
      Get.context,
      HttpAddress.ADD_ACCOUNT,
      {
        "superId": superCompanyId,
        "code": accountCode,
        "name": accountName,
      },
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          return;
        }
        ToastUtils.toast("添加成功");
        accountId = data.toString();
        Get.back(result: {
          Constant.PARAM_COMPANY_ID: accountId,
          Constant.PARAM_COMPANY_NAME: accountName,
          Constant.PARAM_COMPANY_CODE: accountCode,
        });
      },
    );
  }
}
